package com.yanqu.road.server.channelserver.cmd.activity.entername;

import com.yanqu.road.entity.activity.entername.UserEnterName;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.cross3.activity.EnterNameProto;
import com.yanqu.road.server.channelserver.cmd.ChannelCommand;
import com.yanqu.road.server.channelserver.logic.ChannelMessageHelper;
import com.yanqu.road.server.channelserver.manager.activity.entername.ChannelEnterNameMgr;
import com.yanqu.road.server.channelserver.manager.activity.entername.EnterNameModel;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.protocol.ChannelProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = ChannelProtocol.N_ENTER_NAME_GET_DISPERSE_GOLD_REWARD, desc = "送财领奖")
public class ChannelEnterNameGetDisperseGoldRewardCmd extends ChannelCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        EnterNameProto.ChannelGetEnterNameDisperseGoldRewardReqMsg reqMsg = EnterNameProto.ChannelGetEnterNameDisperseGoldRewardReqMsg.parseFrom(packet.getBytes());
        int activityId = reqMsg.getActivityId();
        EnterNameProto.GetEnterNameDisperseGoldRewardRespMsg.Builder builder = EnterNameProto.GetEnterNameDisperseGoldRewardRespMsg.newBuilder();
        EnterNameModel model = ChannelEnterNameMgr.getEnterNameModel(activityId);
        if(model != null){
            long userId = reqMsg.getUserKey().getUserId();
            UserEnterName userEnterName = model.getUserEnterName(userId);
            if(userEnterName == null){
                builder.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
            }else if(userEnterName.isGetDisperseGold()){
                builder.setRet(GameErrorCode.E_ENTER_NAME_GOLD_REWARD_HAD_GET);
            }else {
                userEnterName.setGetDisperseGold(true);
                builder.setRet(0);
            }
        } else {
            builder.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
        }

        ChannelEnterNameMgr.getLogger().info("enter name activity {} user {} disperse gold ret {}", activityId, reqMsg.getUserKey().getUserId(), builder.getRet());

        if(builder.getRet() == 0){
            EnterNameProto.GetEnterNameDisperseGoldRewardReqMsg.Builder gameMsg = EnterNameProto.GetEnterNameDisperseGoldRewardReqMsg.newBuilder();
            gameMsg.setActivityId(activityId);
            ChannelMessageHelper.sendGamePacket(Protocol.S_ENTER_NAME_GET_DISPERSE_GOLD_FROM_CHANNEL,
                    gameMsg,
                    reqMsg.getUserKey().getChannelId(), reqMsg.getUserKey().getServerId(), reqMsg.getUserKey().getUserId());
        }else {
            ChannelMessageHelper.sendGamePacket(Protocol.U_ENTER_NAME_DISPERSE_GOLD_GET,
                    builder,
                    reqMsg.getUserKey().getChannelId(), reqMsg.getUserKey().getServerId(), reqMsg.getUserKey().getUserId());
        }

    }

    @Override
    public void sendData() {

    }
}
